package com.gitee.wsl.compose.modifier


import androidx.annotation.FloatRange
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.graphics.GraphicsLayerScope
import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.MeasureResult
import androidx.compose.ui.layout.MeasureScope
import androidx.compose.ui.layout.layout
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.DpOffset
import com.gitee.wsl.unit.angle.AngleUnit

interface ModifierLayoutScope: ModifierScope {
    fun layout(
        measure: MeasureScope.(Measurable, Constraints) -> MeasureResult
    ){
        modifier = modifier.layout(measure)
    }

    fun layoutId(layoutId: Any){
        modifier = modifier.layoutId(layoutId)
    }

    fun rotate(degrees: Float){
        modifier = modifier.rotate(degrees)
    }

    fun rotate(angleUnit: AngleUnit) {
        this.rotate(angleUnit.degrees.toFloat())
    }

    //z: (0-360)
    fun rotate(rotationX :Float,
               rotationY :Float,
               rotationZ :Float = 0f,
               transformOrigin: TransformOrigin = TransformOrigin.Center
               ){
        modifier = modifier
            .graphicsLayer {
                this.rotationX = rotationX
                this.rotationY = rotationY
                this.rotationZ = rotationZ
                this.transformOrigin=transformOrigin
            }
    }

    fun aspectRatio(@FloatRange(from = 0.0, fromInclusive = false)
                    ratio: Float,
                    matchHeightConstraintsFirst: Boolean = false){
        modifier = modifier.aspectRatio(ratio, matchHeightConstraintsFirst)
    }

    fun translation(translation:DpOffset){
        modifier = modifier.graphicsLayer {
            this.translationX = translation.x.toPx()
            this.translationY = translation.y.toPx()
        }
    }

    fun graphicsLayer(block: GraphicsLayerScope.() -> Unit){
        modifier = modifier.graphicsLayer(block)
    }

}

class ModifierLayoutScopeImp(override var modifier: Modifier): ModifierLayoutScope

fun Modifier.layout(block:ModifierLayoutScope.()->Unit):Modifier = ModifierLayoutScopeImp(this).apply(block).modifier
